<template>
  <div class="allocation-container">
    <div class="header">
      <button class="nav-button" @click="goToPreviousStep">上一步</button>
      <div class="steps-container">
        <div v-for="(step, index) in steps" :key="step" :class="['step-box', { active: index === 0 }]">
          {{ step }}
        </div>
      </div>
      <button class="nav-button" @click="goToNextStep">下一步</button>
    </div>

    <div class="table-container">
      <div class="table-header">
        <span class="table-title">分配方法选择</span>
        <el-button type="primary" :disabled="!isAllMethodsSelected" @click="handleConfirm"> 确认选择并继续 </el-button>
      </div>

      <div class="allocation-methods">
        <div class="methods-column">
          <div class="method-card">
            <div class="card-header">
              <el-icon><component :is="OfficeBuilding" /></el-icon>
              <h3>组织层分配方法</h3>
            </div>
            <el-divider />
            <div class="method-options">
              <el-radio-group v-model="allocationMethods.organization">
                <el-radio :value="'预估能耗'">预估能耗</el-radio>
                <el-radio :value="'专家意见'">专家意见</el-radio>
                <el-radio :value="'额定功率'">额定功率</el-radio>
              </el-radio-group>
            </div>
          </div>

          <div class="method-card">
            <div class="card-header">
              <el-icon><component :is="SetUp" /></el-icon>
              <h3>产线层分配方法</h3>
            </div>
            <el-divider />
            <div class="method-options">
              <el-radio-group v-model="allocationMethods.productionLine">
                <el-radio :value="'产品产量'">产品产量</el-radio>
                <el-radio :value="'专家意见'">专家意见</el-radio>
              </el-radio-group>
            </div>
          </div>
        </div>

        <div class="methods-column">
          <div class="method-card">
            <div class="card-header">
              <el-icon><component :is="Box" /></el-icon>
              <h3>产品层分配方法</h3>
            </div>
            <el-divider />
            <div class="method-options">
              <el-radio-group v-model="allocationMethods.product">
                <el-radio :value="'产品产量'">产品产量</el-radio>
                <el-radio :value="'产品生产时长'">产品生产时长</el-radio>
              </el-radio-group>
            </div>
          </div>

          <div class="method-card">
            <div class="card-header">
              <el-icon><component :is="Monitor" /></el-icon>
              <h3>设备层分配方法</h3>
            </div>
            <el-divider />
            <div class="method-options">
              <el-radio-group v-model="allocationMethods.equipment" disabled>
                <el-radio :value="'预估能耗'">预估能耗</el-radio>
              </el-radio-group>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts" name="Allocation">
import { ref, computed, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { OfficeBuilding, SetUp, Box, Monitor } from "@element-plus/icons-vue";
import { useTabsStore } from "@/stores/modules/tabs";
const tabStore = useTabsStore();

const router = useRouter();
const route = useRoute();
const projectId = route.params.projectId;

// 定义步骤
const steps = ["分配参数配置", "选择碳排放因子", "碳足迹计算结果"];

// 分配方法选择状态
const allocationMethods = ref({
  organization: "" as string,
  productionLine: "" as string,
  product: "" as string,
  equipment: "预估能耗"
});

// 初始化选择的分配方法
onMounted(() => {
  const savedMethods = localStorage.getItem(`allocationMethods_${projectId}`);
  if (savedMethods) {
    const methods = JSON.parse(savedMethods);
    allocationMethods.value = {
      ...allocationMethods.value,
      ...methods
    };
  }
});

// 判断是否所有方法都已选择
const isAllMethodsSelected = computed(() => {
  return Object.values(allocationMethods.value).every(method => method !== "");
});

// 确认选择
const handleConfirm = () => {
  localStorage.setItem(`allocationMethods_${projectId}`, JSON.stringify(allocationMethods.value));
  if (route.meta.isAffix) return;
  tabStore.removeTabs(route.fullPath);
  router.push(`/lca/impact/allocationParams/${projectId}`);
};

// 上一步
const goToPreviousStep = () => {
  if (route.meta.isAffix) return;
  tabStore.removeTabs(route.fullPath);
  router.push(`/lca/inventory/materialIndex/${projectId}`);
};

// 下一步
const goToNextStep = () => {
  if (route.meta.isAffix) return;
  tabStore.removeTabs(route.fullPath);
  router.push(`/lca/impact/index/${projectId}`);
};
</script>

<style scoped>
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  margin-bottom: 16px;
  background-color: #ffffff;
  border-bottom: 1px solid #dddddd;
  border-radius: 8px;
}
.nav-button {
  padding: 10px 20px;
  font-size: 16px;
  color: white;
  cursor: pointer;
  background-color: #009688;
  border: none;
  border-radius: 5px;
  transition: background-color 0.3s;
}
.nav-button:hover {
  background-color: #007d6a;
}
.steps-container {
  display: flex;
  justify-content: space-between;
  width: 80%;
  padding: 0 20px;
}
.step-box {
  position: relative;
  flex: 1;
  padding: 15px 10px;
  margin: 0 10px;
  color: #777777;
  text-align: center;
  cursor: pointer;
  background-color: #f9f9f9;
  border: 1px solid #dcdcdc;
  border-radius: 5px;
  transition: all 0.3s;
}
.step-box.active {
  font-weight: bold;
  color: #009688;
  background-color: #e6f5f3;
  border-color: #009688;
}
.step-box::after {
  position: absolute;
  top: 50%;
  right: -20px;
  font-size: 20px;
  color: #009688;
  content: "→";
  transform: translateY(-50%);
}
.step-box:last-child::after {
  visibility: hidden;
}
.table-container {
  padding: 20px;
  background-color: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
}
.table-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 12px;
  margin-bottom: 20px;
  border-bottom: 1px solid var(--el-border-color-light);
}
.table-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--el-text-color-primary);
}
.allocation-methods {
  display: flex;
  gap: 24px;
  padding: 20px;
  border: 1px solid #dcdcdc;
  border-radius: 8px;
}
.methods-column {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 24px;
}
.method-card {
  display: flex;
  flex-direction: column;
  height: calc(50% - 12px);
  padding: 24px;
  background-color: #ffffff;
  border: 1px solid var(--el-border-color-light);
  border-radius: 8px;
}
.card-header {
  display: flex;
  gap: 12px;
  align-items: center;
}
.card-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--el-text-color-primary);
}
.card-header .el-icon {
  font-size: 20px;
  color: var(--el-color-primary);
}
.method-options {
  display: flex;
  flex: 1;
  align-items: center;
  padding-top: 20px;
}
.el-radio-group {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  width: 100%;
}
:deep(.el-radio) {
  display: flex;
  align-items: center;
  height: 40px;
  padding: 0 16px;
  margin: 0;
  border: 1px solid var(--el-border-color);
  border-radius: 4px;
  transition: all 0.3s;
}
:deep(.el-radio.is-checked) {
  color: var(--el-color-primary);
  background-color: var(--el-color-primary-light-9);
  border-color: var(--el-color-primary);
}
:deep(.el-radio:not(.is-disabled):hover) {
  border-color: var(--el-color-primary);
}
:deep(.el-radio__label) {
  font-size: 14px;
}
</style>
